package vtt.android.dapanhc;

import vtt.android.dapanhc.utils.VttAlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

public class MyBroadcastReceiver extends BroadcastReceiver {

	Context mContext;

	@Override
	public void onReceive(Context ctx, Intent intent) {
		mContext = ctx;
		if(intent != null && Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){
			SharedPreferences sharedPreferences = ctx.getSharedPreferences("DapAnHC", Context.MODE_PRIVATE);
			boolean isStartAlarm = sharedPreferences.getBoolean(MainActivity.KEY_IS_START_ALARM, false);
			if(!isStartAlarm){
				// set repeat alarm everyday for showing notification
				VttAlarmManager.startAlarmEveryday(mContext, 1, MyBroadcastReceiver.class);
				sharedPreferences.edit().putBoolean(MainActivity.KEY_IS_START_ALARM, true).commit();
				//Log.d("TEST", "VTT>>> onReceive return");
				return;
			}
		}
		//Log.d("TEST", "VTT>>> onReceive showNotification");
		showNotification();
	}

	@SuppressWarnings("deprecation")
	private void showNotification() {
		NotificationManager notificationManager = (NotificationManager) mContext
				.getSystemService(Context.NOTIFICATION_SERVICE);

		Intent intent1 = new Intent(mContext.getApplicationContext(), MainActivity.class);

		Notification notification = new Notification(R.drawable.ic_launcher, mContext.getString(R.string.app_name), System.currentTimeMillis());
		intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

		PendingIntent pIntent = PendingIntent.getActivity(mContext, 0, intent1,
				PendingIntent.FLAG_UPDATE_CURRENT);
		notification.flags |= Notification.FLAG_AUTO_CANCEL;
		notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), mContext.getString(R.string.msg_alert_new_lesson), pIntent);

		notificationManager.notify(0, notification);
	}

}
